﻿/*
Copyright (c) 2007-2011 William POTTIER - AllProgrammic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
#if DEBUG
using System.Diagnostics;
#endif

namespace NetAccess.Common
{
    /// <summary>
    /// Log manager
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// Singletin Instance
        /// </summary>
        public static Logger Instance { get { return Singleton<Logger>.Instance; } }

        private Logger() { }

        /// <summary>
        /// Is logging enable
        /// </summary>
        /// <returns>True if logging is enable</returns>
        public virtual bool IsLoggingEnabled() {
#if DEBUG
            return true; 
#else
            return false;
#endif

        }

        internal void Debug(string message, IContextLogger context = null) { PropagateLog(LogLevel.Debug, message, context); }
        internal void Debug(Exception ex, IContextLogger context = null) { PropagateLog(LogLevel.Debug, String.Format("An exception occured : {0}", ex), context); }

        internal void Info(string message, IContextLogger context = null) { PropagateLog(LogLevel.Info, message, context); }
        internal void Info(Exception ex, IContextLogger context = null) { PropagateLog(LogLevel.Info, String.Format("An exception occured : {0}", ex), context); }

        internal void Warning(string message, IContextLogger context = null) { PropagateLog(LogLevel.Warning, message, context); }
        internal void Warning(Exception ex, IContextLogger context = null) { PropagateLog(LogLevel.Warning, String.Format("An exception occured : {0}", ex), context); }
        
        internal void Error(string message, IContextLogger context = null) { PropagateLog(LogLevel.Error, message, context); }
        internal void Error(Exception ex, IContextLogger context = null) { PropagateLog(LogLevel.Error, String.Format("An exception occured : {0}", ex), context); }

        protected void PropagateLog(LogLevel level, string message, IContextLogger context = null)
        {
            if (!IsLoggingEnabled())
                return;

#if DEBUG
            var previewStackFrame = new StackFrame(2, true);
            var method = previewStackFrame.GetMethod();
            var file = previewStackFrame.GetFileName();
            if(context == null)
                Console.WriteLine("[{0}] {1} | {2} in {3} ", level, message, method, file);
            else
                Console.WriteLine("[{0}][{1}] {2} | {3} in {4} ", context.GetName(), level, message, method, file);
#endif
            OnLogging(level, message, context);
        }

        protected virtual void OnLogging(LogLevel level, string message, IContextLogger context = null) { }
    }
}
